package me.mouse.gokistats.talent;

import java.io.File;
import java.io.IOException;
import java.util.Set;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.inventory.ItemStack;

import me.mouse.gokistats.GokiStats;
import me.mouse.gokistats.config.Config;

public class TalentConfig {
	public static boolean betterConfig;

	public static void load() {
		FileConfiguration talentConfig = GokiStats.load("talent.yml");

		betterConfig = talentConfig.getBoolean("betterConfig", false);

		if (!betterConfig) {
			return;
		}
		Set<String> talentSet = talentConfig.getConfigurationSection("").getKeys(false);
		for (String s : talentSet) {
			String name = talentConfig.getString(s + ".name");
			int id = talentConfig.getInt(s + ".id");
			boolean enable = talentConfig.getBoolean(s + ".enable");
			String displayerName = talentConfig.getString(s + ".displayerName");
			int slot = talentConfig.getInt(s + ".slot");
			int maxLevel = talentConfig.getInt(s + ".maxLevel");
			ItemStack item = talentConfig.getItemStack(s + ".item");
			int payment = talentConfig.getInt(s + ".payment", -1);
			String formula = talentConfig.getString(s + ".formula");
			Talent t = new Talent(name, id);
			t.setEnable(enable);
			t.setDisplayName(displayerName);
			t.setSlot(slot);
			t.setMaxLevel(maxLevel);
			t.setItem(item);
			if ((payment >= 5) || (payment <= -1))
				t.setPayment(Config.getPaymentType());
			else {
				t.setPayment(GokiStats.getPayment(payment));
			}
			if (formula != null)
				t.setFormula(formula);
			else {
				t.setFormula(Config.getFormula());
			}
			RegisterTalent.registerTalent(new Talent[] { t });
		}
	}

	public static void save(Talent[] t) {
		FileConfiguration talentConfig = GokiStats.load("talent.yml");
		Talent[] arrayOfTalent = t;
		int j = t.length;
		for (int i = 0; i < j; i++) {
			Talent talent = arrayOfTalent[i];
			String name = talent.getName();
			talentConfig.set(name + ".name", talent.getName());
			talentConfig.set(name + ".id", Integer.valueOf(talent.getID()));
			talentConfig.set(name + ".enable", Boolean.valueOf(talent.getEnable()));
			talentConfig.set(name + ".displayerName", talent.getDisplayName());
			talentConfig.set(name + ".slot", Integer.valueOf(talent.getSlot()));
			talentConfig.set(name + ".maxLevel", Integer.valueOf(talent.getMaxLevel()));
			talentConfig.set(name + ".item", talent.getItem());
			talentConfig.set(name + ".payment", talent.getPayment());
			talentConfig.set(name + ".formula", talent.getFormula());
		}
		try {
			talentConfig.save(new File(GokiStats.file, "talent.yml"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static boolean getBetterConfig() {
		FileConfiguration talentConfig = GokiStats.load("talent.yml");
		talentConfig = GokiStats.load("talent.yml");
		betterConfig = talentConfig.getBoolean("betterConfig", false);
		return betterConfig;
	}
}
